package xyz.ibenben.zhongdian.system.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import xyz.ibenben.zhongdian.common.annotation.SystemControllerLog;
import xyz.ibenben.zhongdian.common.constants.Constants;
import xyz.ibenben.zhongdian.system.entity.RentingHouse;
import xyz.ibenben.zhongdian.system.form.RentingHouseForm;
import xyz.ibenben.zhongdian.system.service.HouseInfoService;
import xyz.ibenben.zhongdian.system.service.RentingHouseService;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 租赁房屋控制类
 * 提供了一些基本的服务，如初始化、获取房屋租赁列表等方法。
 * spring mvc思想 通过controller来调用service里的方法，
 * service里的方法再通过调用Dao来实现对数据的操作
 * 返回值统统是String类型的返回页面有统一的安排方便代码提取
 *
 * @author chenjian
 * @since 2017年9月27日上午10:57:28
 */
@Controller
@SessionAttributes("chatRecordForm")
@RequestMapping("/rentingHouse")
public class RentingHouseController extends BaseController{
	/* 实体名称 */
	private static final String ENTITYNAME = "rentingHouse";

	private final RentingHouseService rentingHouseService;
	private final HouseInfoService houseInfoService;

	@Autowired
	public RentingHouseController(RentingHouseService rentingHouseService, HouseInfoService houseInfoService) {
		this.rentingHouseService = rentingHouseService;
		this.houseInfoService = houseInfoService;
	}

	/**
	 * 初始化
	 * @param map 参数
	 * @param rentingHouseForm 条件参数
	 */
	@ModelAttribute
	public void init(Model map, HttpServletRequest request, @ModelAttribute RentingHouseForm rentingHouseForm){
		//初始化数据
		Long userId = (Long)request.getSession().getAttribute(Constants.SESSIONID);
		map.addAttribute("houseInfoList", houseInfoService.findByCreateIdAndType(userId, 3));
		//初始化条件放到Session里
		RentingHouseForm newRentingHouseForm = new RentingHouseForm();
		if(rentingHouseForm != null){
			newRentingHouseForm = rentingHouseForm;
		}
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newRentingHouseForm);
	}

	/**
	 * 获取房屋租赁列表
	 * @param map 参数
	 * @param rentingHouseForm 条件参数
	 * @return 页面
	 */
	@RequestMapping(value="/getall")
	@SystemControllerLog(description = "获取房屋租赁列表")
	public String getall(Model map, @ModelAttribute RentingHouseForm rentingHouseForm){
		//获取到的条件重新放入session
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), rentingHouseForm);
		//获取页面信息
		Page<RentingHouse> page = PageHelper.startPage(rentingHouseForm.getPageIndex(), rentingHouseForm.getPageSize());
		//获取列表信息
		List<RentingHouse> list = rentingHouseService.selectByExample(rentingHouseForm.buildSearchCommond());
		map.addAttribute(String.format(Constants.LISTENTITY, ENTITYNAME), list);
		map.addAttribute(Constants.PI, page.toPageInfo());
		return String.format(Constants.LISTPAGE, ENTITYNAME);
	}

	/**
	 * 删除房屋租赁记录
	 * @param id 租赁主键
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping(value = "/{id}/delete")
	@SystemControllerLog(description = "删除房屋租赁记录")
	public String delete(@PathVariable Long id, HttpServletRequest request) {
		//逻辑删除数据
		rentingHouseService.delete(id, request);
		return String.format(Constants.REDIRECT, ENTITYNAME);
	}

	/**
	 * 删除房屋租赁列表
	 * @param rentingHouseForm 条件参数
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping("/deleteall")
	@SystemControllerLog(description = "删除房屋租赁列表")
	public String deleteall(@ModelAttribute RentingHouseForm rentingHouseForm,
			HttpServletRequest request) {
		//根据选择的ids删除数据
		if(rentingHouseForm.getSelectIds() != null){
			String[] ids = rentingHouseForm.getSelectIds().split(Constants.COMMA);
			for(String id: ids){
				rentingHouseService.delete(Long.parseLong(id), request);
			}
		}
		return String.format(Constants.REDIRECT, ENTITYNAME);
	}

	/**
	 * 打开房屋租赁添加页面
	 * @param map 参数
	 * @return 页面
	 */
	@RequestMapping(value="/add", method= RequestMethod.GET)
	@SystemControllerLog(description = "打开房屋租赁添加页面")
	public String openAddPage(Model map){
		//创建新的实体到页面
		map.addAttribute(ENTITYNAME, new RentingHouse());
		return String.format(Constants.ADDPAGE, ENTITYNAME);
	}

	/**
	 * 保存房屋租赁添加记录
	 * @param t 实体
	 * @param bindingResult 校验
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping(value="/add", method= RequestMethod.POST)
	@SystemControllerLog(description = "保存房屋租赁添加记录")
	public String postAddMessage(@Valid @ModelAttribute RentingHouse t,
			BindingResult bindingResult, HttpServletRequest request){
		if (!bindingResult.hasErrors()) {
			//保存实体
			rentingHouseService.save(t, request);
			return String.format(Constants.REDIRECT, ENTITYNAME);
		}
		return String.format(Constants.ADDPAGE, ENTITYNAME);
	}

	/**
	 * 打开房屋租赁修改页面
	 * @param map 参数
	 * @param id 租赁主键
	 * @return 页面
	 */
	@RequestMapping(value="/{id}/edit", method= RequestMethod.GET)
	@SystemControllerLog(description = "打开房屋租赁修改页面")
	public String openEditPage(Model map, @PathVariable Long id){
		//获取实体通过主键
		map.addAttribute(ENTITYNAME, rentingHouseService.findOneWithImage(id));
		return String.format(Constants.EDITPAGE, ENTITYNAME);
	}

	/**
	 * 更新房屋租赁修改记录
	 * @param t 实体
	 * @param bindingResult 校验
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping(value="/edit", method= RequestMethod.POST)
	@SystemControllerLog(description = "更新房屋租赁修改记录")
	public String postEditMessage(@Valid @ModelAttribute RentingHouse t,
			BindingResult bindingResult, HttpServletRequest request){
		if (!bindingResult.hasErrors()) {
			//更新实体
			rentingHouseService.updateNotNull(t, request);
			return String.format(Constants.REDIRECT, ENTITYNAME);
		}
		return String.format(Constants.EDITPAGE, ENTITYNAME);
	}

	/**
	 * 浏览房屋租赁记录
	 * @param map 参数
	 * @param id 租赁主键
	 * @param rentingHouseForm 条件参数
	 * @return 页面
	 */
	@RequestMapping(value="/{id}/view", method= RequestMethod.GET)
	@SystemControllerLog(description = "浏览房屋租赁记录")
	public String openViewPage(Model map, @PathVariable Long id,
			 @ModelAttribute RentingHouseForm rentingHouseForm){
		//获取租赁信息
		map.addAttribute(ENTITYNAME, rentingHouseService.findOneWithImage(id));
		return String.format(Constants.VIEWPAGE, ENTITYNAME);
	}
	
}

